package com.tbs.easyhomemoneyserver.model.dto

import com.tbs.easyhomemoneyserver.exception.LoginRollBackException
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty
import java.io.Serializable

@ApiModel
data class ResultVO<T>(
    @ApiModelProperty("请求返回的数据")
    val data: T? = null,

    @ApiModelProperty("请求状态 200成功 400错误 500 警告")
    val status: Long = 0,

    @ApiModelProperty("附加消息文本")
    val msg: String? = null
) : Serializable {


    companion object {
        const val SUCCESS: Long = 200
        const val ERROR: Long = 400

        const val LOGIN_ERROR: Long = 600

        const val WARN: Long = 500
        private const val serialVersionUID = -795000530043039958L

        @JvmStatic
        fun <Y> make(data: Y?, msg: String?, status: Long): ResultVO<Y?> {
            return ResultVO<Y?>(data, status, msg)
        }

        @JvmStatic
        fun loginError(loginRollBackException: LoginRollBackException): ResultVO<LoginRollBackException?> {
            return ResultVO(loginRollBackException, LOGIN_ERROR, loginRollBackException.message)
        }

        @JvmStatic
        fun <Y> success(data: Y? = null, msg: String? = "调用成功"): ResultVO<Y?> {
            return make(data, msg, SUCCESS)
        }

        @JvmStatic
        fun error(throwable: Throwable): ResultVO<Throwable?> {
            return make(throwable, throwable.message, ERROR)
        }

    }
}
